<?php

/*

    MMetaverse - Open Metaverse Access Library

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

require_once dirname(__FILE__) . '/../client/MMetaverseClient.class.php';
require_once dirname(__FILE__) . '/../types/MUUID.class.php';

/**
 * @package mjs.mmetaverse.inventory
 */

require_once 'MFolder.class.php';

/**
 * User Inventory 
 */
class MInventory {

    /**
     * Metaverse client 
     * 
     * @var MMetaverseClient
     */
    private $metaverseClient;

    /**
     * Inventory manager
     *
     * @var MInventoryManager
     */
    private $inventoryManager;

    /**
     * The root folder
     *
     * @var MFolder
     */
    private $rootFolder;

    /**
     * Create an empty inventory
     *
     * @param MMetaverseClient Metaverse client
     * @param MInventoryManager Inventory Manager 
     */
    public function MInventory(MMetaverseClient $metaverseClient = null,
        MInventoryManager $inventoryManager = null) {

        $this->metaverseClient = $metaverseClient;
        $this->inventoryManager = $inventoryManager;
    }
    
    /** 
     * Update the root folder of the inventory 
     * 
     * @param MFolder $rootFolder The root folder
     */
    public function setRootFolder($rootFolder)
    {
        $this->rootFolder = $rootFolder;
    }

    /**
     * Add the folder to the inventory
     *
     * @param MFolder $folder New folder
     */
    private function addFolder(MFolder $folder) {
        // Create the zero folder
        $zero = new MUUID();

        // If the parent is not zero add to root the folder
        if($folder->getParentID() != $zero) {
            $this->rootFolder->addFolder($folder);
        }
    }

    /**
     * Load the inventory data
     *
     * @param array $response Login Response
     */
    public function load(array $response) {
        // Get Inventory Skeleton
        $skeleton = $response[0]['inventory-skeleton'];

        // Note : The SecondLife simulator don't send the root folder first 
        // Get the root folder, the folder with parent-id zero
        foreach($skeleton as $dir) {
            // Check if the folder have uuid zero 
            if($dir['parent_id'] == '00000000-0000-0000-0000-000000000000') {
                // Create folder
                $folder = new MFolder(
                    new MUUID($dir['folder_id']),$dir['name'],
                    new MUUID($dir['parent_id']),$dir['type_default'],
                    $dir['version']);

                // Add the folder to the inventory
                $this->rootFolder = $folder;
            }
        }
        
        // Create a new folder
        foreach($skeleton as $dir) {
            // Create folder 
            $folder = new MFolder(
                new MUUID($dir['folder_id']),$dir['name'],
                new MUUID($dir['parent_id']),$dir['type_default'],
                $dir['version']);

            // Add the folder to the inventory
            $this->addFolder($folder);
        }
    }

    /**
     * Returns the subfolder with the given name
     *
     * @param string $folderName folder name
     * @return MFolder The folder or null if not available
     */
    public function getFolderByName($folderName) {
        return $this->rootFolder->getFolderByName($folderName);
    }

    /**
     * Upload a texture to the Textures folder
     *
     * @param string $name Texture Name
     * @param string $description Description
     * @param array $content Content
     * @param MUUID the uuid of the texture
     */
    public function uploadTexture($name, $description, $content) {
        // Get capabilities
        $capabilities = $this->metaverseClient->getCapabilities();
        
        // Textures Folder
        $texturesFolder = $this->getFolderByName('Textures');
        
        // Create a nw file
        $response = $capabilities->newFileAgentInventory(
            'texture','texture',$texturesFolder->getID(),
                $name,$description,$content);

        // Return the uuid for the new ite
        return new MUUID($response['new_inventory_item']);
    }
}

?>
